package com.wgu.common.utils;

import com.alibaba.fastjson.JSONObject;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

import java.util.ArrayList;
import java.util.List;


/**
 * 分页工具
 * Created by w on 2019/3/20.
 */
public class PageUtils{

    /**
     * List 分页
     * @param page
     * @param size
     * @param list
     * @return
     */
    public static List toPage(int page, int size , List list) {
        int fromIndex = page * size;
        int toIndex = page * size + size;

        if(fromIndex > list.size()){
            return new ArrayList();
        } else if(toIndex >= list.size()) {
            return list.subList(fromIndex,list.size());
        } else {
            return list.subList(fromIndex,toIndex);
        }
    }

    /**
     * 将结果为Page的对象封装成JSON格式返回
     * @return
     */
    static public JSONObject resultWrap(int size , List list,long count){
        JSONObject result = new JSONObject();
        result.put("content",list);
        result.put("pages",0);
        result.put("total",count);
        if(list==null||list.size()==0) return result;
        long pages = count/size + 1;
        result.put("pages",pages);
        return  result;
    }

    /**
     * 将结果为Page的对象封装成JSON格式返回
     * @return
     */
    static public JSONObject resultWrap(Page pageResult){
        if(pageResult==null) return null;
        JSONObject result = new JSONObject();
        result.put("content",pageResult.getContent());
        result.put("pages",pageResult.getTotalPages());
        result.put("total",pageResult.getTotalElements());
        return  result;
    }

    /**
     * 将结果为Page的对象封装成JSON格式返回
     * @param pageResult 分页
     * @param object content返回对象
     * @return
     */
    static public JSONObject resultWrap(Page pageResult, Object object){
        if(pageResult==null) return null;
        JSONObject result = new JSONObject();
        result.put("content",object);
        result.put("total",pageResult.getTotalElements());
        result.put("pages",pageResult.getTotalPages());
        return  result;
    }

    /**
     * 对Pageable进行多字段排序
     * @param pageable 原pageable
     * @param direction exp : Sort.Direction.DESC 倒叙  Sort.Direction.ASC 正序
     * @param properties 排序字段集合
     * @return
     */
    public static Pageable sortPageable(Pageable pageable, Sort.Direction direction, List<String> properties){
        return PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(),new Sort(direction, properties));
    }

    /**
     * 对Pageable进行单字段排序
     * @param pageable 原pageable
     * @param direction exp : Sort.Direction.DESC 倒叙  Sort.Direction.ASC 正序
     * @param properties 排序字段 exp："id"
     * @return
     */
    public static Pageable sortPageable(Pageable pageable, Sort.Direction direction, String properties){
        return PageRequest.of(pageable.getPageNumber(), pageable.getPageSize(),new Sort(direction, properties));
    }

}
